package com.rz.paas.test.inChannel

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.rz.paas.test.inChannel.adapter.VideoListAdapter

class VideoToppingDirection(val context: Context) :
    RecyclerView.ItemDecoration() {


    val itemHeaderHeight by lazy {
        dp2px(200)
    }

    val itemHeaderPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }

    val textPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }

    private fun dp2px(dp: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dp * 1.0f,
            context.resources.displayMetrics
        )
    }

    init {
        itemHeaderPaint.color = Color.GRAY
        textPaint.textSize = 46f
        textPaint.color = Color.RED
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.adapter is VideoListAdapter) {
            var adapter = parent.adapter as VideoListAdapter
            var childCount = parent.childCount

        }

    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
//        if (parent.adapter is VideoListAdapter) {
//            val adapter = parent.adapter as VideoListAdapter
//
//            val top = parent.paddingTop
//            val left = parent.paddingLeft
//            val right = parent.width - parent.paddingRight
//            val videoData = adapter.data[0] as VideoData
//            if (videoData.isTopping) {
//                c.drawRect(
//                    left.toFloat(), top.toFloat(),
//                    right.toFloat(), top + itemHeaderHeight.toFloat(),
//                    itemHeaderPaint
//                )
//            }
//
//            c.save()
//        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is VideoListAdapter) {
            val adapter = parent.adapter as VideoListAdapter
            val position = parent.getChildLayoutPosition(view)
            if (position%2 == 0) {
//                outRect.top = itemHeaderHeight.toInt()
                outRect.right = 1
            }
            if (position>1){
                outRect.top = 1
            }
        }
    }

}